#ifndef NETWORK_H_20120411
#define NETWORK_H_20120411

////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 此函数用于RAKNET通信，CNetwork为通信基类
//
//                                             Wander 2012.04.12
/////////////////////////////////////////////////////////////////////////////////////////////////////


#include "RakNetSources/BitStream.h"
#include "RakNetSources/RakNetTypes.h"
#include "RakNetSources/MessageIdentifiers.h"
#include "RakNetSources/RakThread.h"

#include "GamePackType.h"

#include "cocos2d.h"
#include "pthread.h"

#include <queue>

const int NETWORK_STRINGLEN_MAX = 32;
class CNetworkBase
{
public:
	void Connect(const std::string strIP);
    
	// 在断开连接前，清空队列信息和连接信息
	void ReleaseConnect();
    
	// 获取队列中得一个包，获取后，此包仍在队列中，需要手动删除
	// 返回NULL，获取失败
	RakNet::Packet *GetTopPacket();
	
	void ReceivePacket();
	// 移除一个包
	void RemoveTopPacket();
    
	bool SendPacketToServer(const RakNet::BitStream &bsSend);
protected:
    
	virtual void startConnect(const std::string strIP) = 0;
    
public:
    
	RakNet::RakPeerInterface    *m_pPeer;
	pthread_t                    m_threadID;
	std::queue<RakNet::Packet *> m_qPackets;
	RakNet::RakNetGUID           m_serverID;
	std::string                  m_strServerIP;
};

class CAccountServer : public CNetworkBase
{
public:
    
	static CAccountServer *GetInstance();
    
private:
    
	virtual void startConnect(const std::string strIP);
    
	static  void *ThreadAccountPacket(void *arg);
    //static RAK_THREAD_DECLARATION(ThreadAccountPacket);
private:
    
	static CAccountServer *m_pInstance;
};

class CGameServer : public CNetworkBase
{
public:
	static CGameServer *GetInstance();
    
private:
    
	virtual void startConnect(const std::string strIP);
	static  void *ThreadGamePacket(void *arg);
    
    //static RAK_THREAD_DECLARATION(ThreadGamePacket);
    
private:
    
	static CGameServer *m_pInstance;
};

#endif